package flc.ast.fragment.date;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tyme.lunar.LunarDay;
import com.tyme.solar.SolarDay;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import flc.ast.R;
import flc.ast.databinding.FragmentDateConvertBinding;
import flc.ast.dialog.SelDateDialog;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DateConvertFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DateConvertFragment extends BaseNoModelFragment<FragmentDateConvertBinding> {

    private SelDateDialog selDateDialog;
    private int mSelYear;
    private int mSelMonth;
    private int mSelDay;

    @Override
    protected int onCreate() {
        return R.layout.fragment_date_convert;
    }

    @Override
    protected void initView() {
        mDataBinding.llCurrentDate.setOnClickListener(this);
        mDataBinding.tvStartConvert.setOnClickListener(this);

        Date currentDate = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);

        mSelYear = calendar.get(Calendar.YEAR);
        mSelMonth = calendar.get(Calendar.MONTH) + 1;
        mSelDay = calendar.get(Calendar.DAY_OF_MONTH);
        mDataBinding.tvCurrentDate.setText(dateFormat.format(currentDate));
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.llCurrentDate://当前日期
                showSelDateDialog();
                break;
            case R.id.tvStartConvert://开始转换
                SolarDay solarDay = SolarDay.fromYmd(mSelYear, mSelMonth, mSelDay);
                LunarDay lunarDay = solarDay.getLunarDay();
                mDataBinding.tvConvertResult.setText(lunarDay.toString().substring(2) + "");
                break;
        }
    }

    @Override
    protected void initData() {

    }

    //选择日期弹框
    private void showSelDateDialog() {
        if (selDateDialog == null) {
            selDateDialog = new SelDateDialog(mContext);
        }
        selDateDialog.title = getString(R.string.start_date);
        selDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void selDate(int year, int month, int day) {
                mSelYear = year;
                mSelMonth = month;
                mSelDay = day;
                String selDate = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
                mDataBinding.tvCurrentDate.setText(selDate);
            }
        });
        selDateDialog.show();
    }

}